﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01_Tower3._0
{
    internal class InfoSystem
    {
        // 单例模式
        // 保证类的实例只有一个 唯一的
        // 能在任何地方调用
        static InfoSystem? instance = null;
        public static InfoSystem Instance
        {
            get
            {
                // 如何保证唯一性 就是让构造函数私有
                // 只能在第一次调用时自己实例化
                if (instance == null)
                {
                    instance = new InfoSystem(30, 0, 12, 20, 50);
                }
                return instance;
            }
        }

        // 获得消息
        // 存储消息
        // 使用字符串数组
        // 打印消息
        struct InfoData
        {
            public string str;
            public ConsoleColor color;
        }


        InfoData[] infoList;
        int index;//指针
        int maxCount;//最大存储量
        int offsetX, offsetY;
        bool isOpen;
        long lastTime, currentTime;
        int delay;
        int row, col;

        protected InfoSystem(int x, int y, int row, int col, int delay)
        {
            Console.CursorVisible = false;
            Console.SetWindowSize(100, 30);
            Console.SetBufferSize(100, 30);
            index = 0;
            maxCount = 10;
            infoList = new InfoData[maxCount];
            offsetX = x;
            offsetY = y;
            isOpen = true;
            this.delay = delay * 10000;
            this.row = row;
            this.col = col;
            lastTime = DateTime.Now.Ticks;
        }

        public void AddInfo(string str, ConsoleColor color = ConsoleColor.White)
        {
            if (index > maxCount - 1)
            {
                index = maxCount - 1;
                FowardInfoArray();
            }
            InfoData info = new InfoData();
            info.str = str;
            info.color = color;
            infoList[index++] = info;
        }

        void FowardInfoArray()
        {
            for (int i = 0; i < maxCount - 1; i++)
            {
                infoList[i] = infoList[i + 1];
            }
        }

        void PrintFrame()
        {
            //MyPrint("                                       ", offsetX, row + 2);
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    string temp = "";
                    if (i == 0 && j == 0)
                    {
                        /*MyPrint("┌", offsetX + j, offsetY + i);*/
                        temp = "┌";
                    }
                    else if (i == 0 && j == col - 1)
                    {
                        /*MyPrint("┐", offsetX + j, offsetY + i);*/
                        temp = "┐";
                    }
                    else if (i == row - 1 && j == 0)
                    {
                        /*MyPrint("└", offsetX + j, offsetY + i);*/
                        temp = "└";
                    }
                    else if (i == row - 1 && j == col - 1)
                    {
                        /*MyPrint("┘", offsetX + j, offsetY + i);*/
                        temp = "┘";
                    }
                    else if (i == 0 || i == row - 1)
                    {
                        /* MyPrint("一", offsetX + j, offsetY + i);*/
                        temp = "─";
                    }
                    else if (j == 0 || j == col - 1)
                    {
                        /* MyPrint("|", offsetX + j, offsetY + i);*/
                        temp = "│";
                    }
                    else
                    {
                        temp = "  ";
                    }
                    MyPrint(temp, offsetX + j, offsetY + i);
                }
            }
        }

        void PrinText()
        {
            for (int i = 0; i < row - 2; i++)
            {
                MyPrint(infoList[i].str, offsetX + 3, offsetY + 1 + i, infoList[i].color);
            }
        }

        public void PrintInfo()
        {
            PrintFrame();
            PrinText();
        }

        void MyPrint(string str, int x, int y, ConsoleColor color = ConsoleColor.White)
        {
            Console.SetCursorPosition(x * 2, y);
            Console.ForegroundColor = color;
            Console.Write(str);
            Console.ResetColor();
        }
    }
}
